﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft_Translator_V2;

namespace Microsoft_Translator_V2
{
    public partial class ms_Translator : Form
    {
        const string XinChenAppID = "9CCDC7C89504F24A249DAFC5AE83517E720AE57D";
        TranslatorService.LanguageServiceClient client;
        string XinChenBingToken;
        string[] langCodes, langNames_cn;
        //string[] langNames_en, langForSpeak;

        public ms_Translator()
        {
            InitializeComponent();
        }

        private void ms_Translator_Load(object sender, EventArgs e)
        {
            try
            {
                client = new TranslatorService.LanguageServiceClient();
            }
            catch
            {
                MessageBox.Show("无法联系微软翻译服务！", "服务出错", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            XinChenBingToken = client.GetAppIdToken(XinChenAppID, 5, 4, 86400);

            langCodes = client.GetLanguagesForTranslate(XinChenBingToken);
            langNames_cn = client.GetLanguageNames(XinChenBingToken, "zh-CHS", langCodes);
            //langNames_en = client.GetLanguageNames(XinChenBingToken, "en", langCodes);
            //langForSpeak = client.GetLanguagesForSpeak(XinChenBingToken);

            int cn_idx = -1, en_idx = -1;
            for (int i = 0; i < langCodes.Length; i++) 
            {
                if (cn_idx > -1 && en_idx > -1) 
                {
                    break;
                }
                if (langCodes[i] == "zh-CHS") 
                {
                    cn_idx = i;
                    continue;
                }
                if (langCodes[i] == "en") 
                {
                    en_idx = i;
                    continue;
                }

            }

            cBox_FromLang.Items.Clear();
            cBox_FromLang.Items.AddRange(langNames_cn);
            cBox_FromLang.SelectedIndex = en_idx;

            cBox_ToLang.Items.Clear();
            cBox_ToLang.Items.AddRange(langNames_cn);
            cBox_ToLang.SelectedIndex = cn_idx;
        }

        private void btn_Translte_Click(object sender, EventArgs e)
        {
            try
            {
                tBox_ToText.Text = client.Translate(XinChenBingToken, tBox_FromText.Text, langCodes[cBox_FromLang.SelectedIndex], langCodes[cBox_ToLang.SelectedIndex], "text/html", "general");
            }
            catch
            {
                MessageBox.Show("无法翻译该文本！", "翻译出错", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
        }

        private void btn_Read_Click(object sender, EventArgs e)
        {
            try
            {
                string audioURL = client.Speak(XinChenBingToken, tBox_ToText.Text, langCodes[cBox_ToLang.SelectedIndex], "audio/wav");
                //System.Diagnostics.Process.Start(audioURL);
                using (System.Media.SoundPlayer player = new System.Media.SoundPlayer())
                {
                    player.SoundLocation = audioURL;
                    player.Play();
                }
            }
            catch
            {
                MessageBox.Show("无法朗读该文本！", "朗读出错", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }

        }
    }
}
